package com.cs.controller;

import com.cs.domain.CSBudget;
import com.cs.domain.CSLineItems;
import com.cs.domain.CSProcurementPlan;
import com.cs.domain.vo.CSLineItemsVO;
import com.cs.domain.vo.CSProcurementPlanVO;
import com.cs.domain.vo.PurchasePlanVO;
import com.cs.domain.vo.SourcingVO;
import com.cs.service.CSProcurementPlanService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresLogin;
import com.ruoyi.common.security.annotation.RequiresRoles;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author：bbdf
 * @Package：com.cs.domain
 * @Project：RuoYi-Cloud
 * @name：CSProcurementPlanMapper
 * @Date：2024-11-11 10:46
 * @Filename：CSProcurementPlanMapper
 */
@Tag(name = "管理后台 - 采购计划")
@RestController
@RequestMapping("/purchasePlan")
public class CSProcurementPlanController extends BaseController {

    @Autowired
    private CSProcurementPlanService service;
    @Autowired
    private RemoteUserService userService;


    @PostMapping("/export")
    public void exportSourcing(HttpServletResponse response,CSProcurementPlan plan){
        List<CSProcurementPlan> planList = service.getPlanList(plan);
        ExcelUtil<CSProcurementPlan> util=new ExcelUtil<>(CSProcurementPlan.class);
        util.exportExcel(response,planList,"寻源数据");
    }

    @GetMapping("/list")
    public TableDataInfo getPlanList(CSProcurementPlan plan) {
        startPage();
        List<CSProcurementPlan> planList = service.getPlanList(plan);
        return getDataTable(planList);
    }

    @GetMapping("/getBudgetCode")
    public R<String> getBudgetCode() {
        return R.ok(service.getBudgetCode(), "success");
    }

    /**
     * 新增(采购计划,预算,行项目)
     *
     * @param plan
     * @return
     */
    @PostMapping("/addPurchasePlan")
    @RequiresRoles(value = {"common"})
    public R<String> addPurchasePlan(@RequestBody PurchasePlanVO plan) {
        service.addPurchasePlan(plan);
        return R.ok("success");
    }

    /**
     * 跟进采购计划编码获得采购计划
     *
     * @param aCode
     * @return
     */
    @GetMapping("/getPurchasePlanByACode/{aCode}")
    public R<PurchasePlanVO> getPurchasePlanByACode(@PathVariable String aCode) {
        return R.ok(service.getPurchasePlanByACode(aCode), "success");
    }

    /**
     * 跟进状态获得采购计划集合
     *
     * @param aCode
     * @return
     */
    @GetMapping("/getPlanByState/{aCode}")
    public R<List<CSProcurementPlanVO>> getPlanByState(@PathVariable String aCode) {
        return R.ok(service.getPlanByState(aCode), "success");
    }

    /**
     * 编辑采购计划
     *
     * @param plan
     * @return
     */
    @PostMapping("/editPlan")
    @RequiresRoles(value = "common")
    public R<Integer> editPlan(@RequestBody PurchasePlanVO plan) {
        if ("".equals(plan.getPlan().getaCode())) {
            return R.fail("请不要再修改中添加数据!!!");
        }
        return R.ok(service.editPlan(plan), "success");
    }

    /**
     * 删除行项目
     *
     * @param vCode
     * @return
     */
    @DeleteMapping("/deleteDevice/{vCode}")
    @RequiresLogin
    public R<Integer> deleteDevice(@PathVariable String vCode) {
        return R.ok(service.deleteDevice(vCode), "success");
    }

    /**
     * 跟进采购计划编码修改采购计划状态(提交)
     *
     * @param plan
     * @return
     */
    @PutMapping("/changeAstateByACode")
    @RequiresRoles(value = {"common"})
    public R<Integer> changeAstateByACode(@RequestBody CSProcurementPlanVO plan) {
        return R.ok(service.changeAstateByACode(plan), "success");
    }


    /**
     * 跟进采购计划编码修改采购计划状态(审核)因为不同角色,所以加了这个
     *
     * @param plan
     * @return
     */
    @PutMapping("/changeAstateByACodeAdmin")
    @RequiresRoles(value = {"admin"})
    public R<Integer> changeAstateByACodeAdmin(@RequestBody CSProcurementPlanVO plan) {
        return R.ok(service.changeAstateByACode(plan), "success");
    }


    /**
     * 采购寻源
     */
    @PutMapping("/submitSourcing")
    @RequiresRoles(value = {"admin"})
    public R<Integer> submitSourcing(@RequestBody SourcingVO sourcingVO) {
        List<SysUser> sysUsers = sourcingVO.getPlan().stream().map(s -> {
            String userName = s.getCreateBy();
            SysUser infoByName = userService.getInfoByName(userName);
            return infoByName;
        }).toList();
        return service.submitSourcing(sourcingVO, sysUsers);
    }


    /**
     * 优先展示未寻源项目
     */
    @GetMapping("/getNotSourcing")
    public TableDataInfo getNotSourcing(CSProcurementPlan plan) {
        startPage();
        List<CSProcurementPlan> planList = service.getNotSourcing(plan);
        return getDataTable(planList);
    }

    /**
     * 首页查询的数量
     * @param state
     * @return
     */
    @PostMapping("/selectCSProcurementPlanCount")
    @RequiresLogin
    public int selectCSProcurementPlanCount(@RequestBody CSProcurementPlan state) {
        return service.selectCSProcurementPlanCount(state);
    }

    /**
     * 编辑采购预算
     * @param state
     * @return
     */
    @PutMapping("/editBudgetForm")
    @RequiresLogin
    public R<Integer> editBudgetForm(@RequestBody CSBudget state) {
        return R.ok(service.editBudgetForm(state));
    }


    /**
     * 删除采购预算
     */
    @DeleteMapping("/deleteBudgetForm/{duId}")
    @RequiresLogin
    public R<Integer> deleteBudgetForm(@PathVariable String duId) {
        return R.ok(service.deleteBudgetForm(duId));
    }

    @GetMapping("/getLineItemsList/{iNumber}")
    public R<List<CSLineItemsVO>> getLineItemsList(@PathVariable String iNumber){
        return R.ok(service.getLineItemsList(iNumber));
    }
}